package com.sh.data.engine.web.webapi.controller.management;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.HiveSiteXmlUploadDTO;
import com.sh.data.engine.application.model.dto.workspace.BaseStorageDTO;
import com.sh.data.engine.application.model.dto.workspace.StorageDTO;
import com.sh.data.engine.application.model.request.management.SchemaQueryRequest;
import com.sh.data.engine.application.model.request.management.StorageConnectionRequest;
import com.sh.data.engine.application.model.request.management.StorageSaveRequest;
import com.sh.data.engine.application.service.management.StorageApplicationService;
import com.sh.data.engine.common.annotation.crypto.EncryptMethod;
import com.sh.data.engine.common.annotation.fuzzyQuery.FuzzyMethod;
import com.sh.data.engine.common.constants.HttpStatus;
import com.sh.data.engine.common.constants.code.BizCodes;
import com.sh.data.engine.common.exception.BusinessException;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/24 11:19
 */
@Validated
@Slf4j
@Tag(name = "管理中心-存储管理", description = "存储管理API")
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageApplicationService storageApplicationService;


    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/saveStorage")
    @Operation(summary = "添加或编辑存储")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.存储)
    @EncryptMethod
//    @Log(
//        business = Business.存储,
//        operation = Operation.ADD_UPDATE)
    public RestResponseEntity<Boolean> saveStorage(@Valid @RequestBody StorageSaveRequest storageRequest) {
        var result = storageApplicationService.saveStorage(storageRequest);
        return new RestResponseEntity<>(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/deleteStorage")
    @Operation(summary = "删除存储")
    @LogExecution(operation = EngineOperation.DEL, business = Business.存储)
//    @Log(
//        business = Business.存储,
//        operation = Operation.DEL)
    public RestResponseEntity<Boolean> deleteStorage(
        @Parameter(description = "存储id", required = true) @RequestParam Long storageId) {
        var result = storageApplicationService.deleteStorage(storageId);
        return new RestResponseEntity<>(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/testStorage")
    @Operation(summary = "测试存储连通性")
    @EncryptMethod
    public RestResponseEntity<Boolean> testStorage(
        @Valid @RequestBody StorageConnectionRequest storageConnectionRequest) {
        var result = storageApplicationService.testStorage(storageConnectionRequest);
        return new RestResponseEntity<>(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/getDetailStorage")
    @Operation(summary = "根据存储id获取详细信息")
    @LogExecution(operation = EngineOperation.SELECT, business = Business.存储)
    public RestResponseEntity<StorageDTO> getDetailStorage(
        @Parameter(description = "存储id", required = true) @RequestParam Long storageId) {
        try {
            StorageDTO storageDTO = storageApplicationService.getDetailStorage(storageId);
            return new RestResponseEntity<>(storageDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(String.format("系统异常:[%s]", e.getMessage()));
        }
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/getListStorage")
    @Operation(summary = "查询存储列表")
    @LogExecution(operation = EngineOperation.SELECT, business = Business.存储)
    @FuzzyMethod(fuzzyParamIndex = 4)
    public RestResponseEntity<PagerResponse<BaseStorageDTO>> getListStorage(
        @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
        @RequestParam(required = false, defaultValue = "1")
        Integer pageNum,
        @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
        @RequestParam(required = false, defaultValue = "10")
        Integer pageSize,
        @Parameter(description = "当前页最后一条数据的id") @RequestParam(required = false) Long lastStorageId,
        @Parameter(description = "数据类型") @RequestParam(required = false) String dsType,
        @Parameter(description = "搜索内容") @RequestParam(required = false) String search) {
        var result = storageApplicationService.listStorage(pageNum, pageSize, lastStorageId, dsType, search);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 0, isAdd = 1)
    @PostMapping("/getSchemas")
    @Operation(summary = "获取PG、SAPHANA的schema")
    @EncryptMethod
    @LogExecution(operation = EngineOperation.SELECT, business = Business.存储)
    public RestResponseEntity<List<String>> getSchemas(@RequestBody SchemaQueryRequest schemaRequest) {
        var result = storageApplicationService.getSchemas(schemaRequest);
        return new RestResponseEntity<>(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/uploadHiveSiteXml")
    @Operation(summary = "上传hive-site.xml文件")
    @LogExecution(operation = EngineOperation.UPLOAD, business = Business.文件)
    public RestResponseEntity<HiveSiteXmlUploadDTO> uploadHiveSiteXml(
        @RequestPart("file") MultipartFile file) {
        if (file.isEmpty() || file.getSize() == 0) {
            throw new BusinessException(BizCodes.REQUEST_PARAMETER_ERROR, HttpStatus.BAD_REQUEST);
        }

        var result = storageApplicationService.uploadHiveSiteXml(file);
        return new RestResponseEntity<>(result);
    }
}
